/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package dawnland02.service;

import java.util.*;

import dawnland02.data.map.model.Matrix;
import dawnland02.data.map.model.MatrixCell;
import dawnland02.model.entity.Entity;
import dawnland02.model.entity.EntityFactory;
import dawnland02.data.map.service.MatrixService;

/**
 * @author Petru Obreja (obrejap@yahoo.com)
 */
public class GeneralService {
    private static Matrix matrix;

    //def MATRIX_FILE_PATH = "{__DIR__}../resources/matrix.xml";
    private static final String MATRIX_FILE_PATH = "f:/work/javafx/dawnland/dawnland02/src/dawnland02/resources/matrix.xml "; //to be modified later, using a ConstantProperties class

    public static Matrix getMatrix(){
        if(matrix == null){
            matrix = MatrixService.readMatrix(MATRIX_FILE_PATH);
        }
        return matrix;
    }

    public static List getClosestPath(Entity entity, Integer startMatrixCellId, Integer endMatrixCellId){
        return getClosestPath(entity, getMatrix().getNode(startMatrixCellId), getMatrix().getNode(endMatrixCellId));
    }

    public static List getClosestPath(Entity entity, Integer startX, Integer startY, Integer endMatrixCellId){
        MatrixCell endMatrixCell = getMatrix().getNode(endMatrixCellId);
        return MatrixService.getClosestPath(entity, getMatrix(), startX, startY, endMatrixCell.getX(), endMatrixCell.getY());
    }

    public static List getClosestPathAsIds(Entity entity, Integer startMatrixCellId, Integer endMatrixCellId){
        List<MatrixCell> matrixCells = getClosestPath(entity, startMatrixCellId, endMatrixCellId);
        List<Integer> path = new LinkedList<Integer>();
        for(MatrixCell matrixCell:matrixCells){
            path.add(matrixCell.getId());
        }
        return path;
    }

    public static List getClosestPath(Entity entity, MatrixCell start, MatrixCell end){
        return MatrixService.getClosestPath(entity, getMatrix(), start.getX(), start.getY(), end.getX(), end.getY());
    }

    public static Entity createEntity(Integer x,Integer y){
        return EntityFactory.createEntity(x, y);
    }
}
